<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Prepare a SQL statement for execution</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.cubrid-prepare" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">cubrid_prepare</h1>
  <p class="verinfo">(PECL CUBRID &gt;= 8.3.0)</p><p class="refpurpose"><span class="refname">cubrid_prepare</span> &mdash; <span class="dc-title">Prepare a SQL statement for execution</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.cubrid-prepare-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>cubrid_prepare</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$conn_identifier</code></span>  
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$prepare_stmt</code></span>  
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$option</code><span class="initializer"> = 0</span></span>  
  ] ) : <span class="type">resource</span></div>

  <p class="para rdfs-comment">
      The <span class="function"><strong>cubrid_prepare()</strong></span> function is a sort of API which represents SQL statements 
      compiled previously to a given connection handle. This pre-compiled SQL statement will be included 
      in the <span class="function"><strong>cubrid_prepare()</strong></span>. 
  </p>
  <p class="para">
      Accordingly, you can use this statement effectively to execute several times repeatedly or to 
      process long data. Only a single statement can be used and a parameter may put a question mark (?) 
      to appropriate area in the SQL statement. Add a parameter when you bind a value in the VALUES 
      clause of INSERT statement or in the WHERE clause. Note that it is allowed to bind a value to a 
      MARK(?) by using the <span class="function"><a href="cubrid_bind.html" class="function">cubrid_bind()</a></span> function only. 
  </p>
 </div>


<div class="refsect1 parameters" id="refsect1-function.cubrid-prepare-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
  <dl>

    
  <dt>
<code class="parameter">conn_identifier</code></dt>

  <dd>
<p class="para">Connection identifier.</p></dd>

   
   
  <dt>
<code class="parameter">prepare_stmt</code></dt>

  <dd>
<p class="para">Prepare query.</p></dd>

   
   
  <dt>
<code class="parameter">option</code></dt>

  <dd>
<p class="para">OID return option CUBRID_INCLUDE_OID.</p></dd>

   
  </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.cubrid-prepare-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    Request identifier, if process is successful; 
  </p>
  <p class="para">
    <strong><code>FALSE</code></strong>, if process is unsuccessful.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.cubrid-prepare-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1142">
   <p><strong>Example #1 <span class="function"><strong>cubrid_prepare()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">cubrid_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">33000</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"demodb"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOD<br /></span><span style="color: #DD0000">SELECT&nbsp;g.event_code,&nbsp;e.name&nbsp;<br />FROM&nbsp;game&nbsp;g&nbsp;<br />JOIN&nbsp;event&nbsp;e&nbsp;ON&nbsp;g.event_code=e.code&nbsp;<br />WHERE&nbsp;host_year&nbsp;=&nbsp;?&nbsp;AND&nbsp;event_code&nbsp;NOT&nbsp;IN&nbsp;(SELECT&nbsp;event_code&nbsp;FROM&nbsp;game&nbsp;WHERE&nbsp;host_year=?)&nbsp;GROUP&nbsp;BY&nbsp;event_code;<br /></span><span style="color: #007700">EOD;<br /><br /></span><span style="color: #0000BB">$req&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">cubrid_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">cubrid_bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2004</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">cubrid_bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2000</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">cubrid_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$row_num&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">cubrid_num_rows</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"There&nbsp;are&nbsp;%d&nbsp;event&nbsp;that&nbsp;exits&nbsp;in&nbsp;2004&nbsp;olympic&nbsp;but&nbsp;not&nbsp;in&nbsp;2000.&nbsp;For&nbsp;example:\n\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$row_num</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%-15s&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Event_code"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Event_name"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"----------------------------\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">cubrid_fetch_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%-15d&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">"event_code"</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">"name"</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">cubrid_fetch_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%-15d&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">"event_code"</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">"name"</span><span style="color: #007700">]);<br /><br /></span><span style="color: #0000BB">cubrid_disconnect</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
There are 27 event that exits in 2004 olympic but not in 2000. For example:

Event_code      Event_name
----------------------------
20063           +91kg
20070           64kg
</pre></div>
   </div>
  </div>
 </div>


  <div class="refsect1 seealso" id="refsect1-function.cubrid-prepare-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">  
    <li class="member"><span class="function"><a href="cubrid_execute.html" class="function" rel="rdfs-seeAlso">cubrid_execute()</a> - Execute a prepared SQL statement</span></li>  
    <li class="member"><span class="function"><a href="cubrid_bind.html" class="function" rel="rdfs-seeAlso">cubrid_bind()</a> - Bind variables to a prepared statement as parameters</span></li>  
   </ul>
  </p>
  </div>


</div></div></div></body></html>